//
//  HosPCommentViewController.m
//  安好
//
//  Created by lianchuang on 14/12/10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "HosPCommentViewController.h"

@interface HosPCommentViewController ()

@end

@implementation HosPCommentViewController

- (void)KeyboardWillHiden
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self KeyboardWillHiden];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"医生评价";
    
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 45, 23)];
    [rightButton setTitle:@"发布" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    self.labelText.hidden=NO;
    self.commentTextView.delegate=self;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark UITextViewDelegate textView的代理方法
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    self.labelText.hidden=YES;//隐藏文字
    if (kScreenHeight==480) {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame=CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height+150);
        }];
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length>0) {
        self.labelText.hidden=YES;
        
    }else{
        self.labelText.hidden=NO;//隐藏文字
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        if (kScreenHeight==480) {
            [UIView animateWithDuration:0.15 animations:^{
                self.view.frame=CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-150);
            }];
        }
        [textView resignFirstResponder];
        return NO;
    }
    if (textView.text.length==0){//textview长度为0
        if ([text isEqualToString:@""]) {//判断是否为删除键
            self.labelText.hidden=YES;//隐藏文字
        }else{
            self.labelText.hidden=YES;
        }
    }else{//textview长度不为0
        if (textView.text.length==1){//textview长度为1时候
            if ([text isEqualToString:@""]) {//判断是否为删除键
                self.labelText.hidden=NO;
            }else{//不是删除
                self.labelText.hidden=YES;
            }
        }else{//长度不为1时候
            self.labelText.hidden=YES;
        }
    }
    return YES;
}

- (void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES ];
}
- (void)rightButtonClicked
{
    [self.commentTextView resignFirstResponder];
    if (self.wholeIndex==0||self.severIndex==0||self.levelIndex==0) {
        [self showCustomHudSingleLine:@"请评价满意程度"];
        return;
    }
    if (self.commentTextView.text.length<=0) {
        [self showCustomHudSingleLine:@"评论内容不能为空"];
        return;
    }
    
    [self showActionView];
    NSString * postURL = [NSString stringWithFormat:@"%@wiki/addHospitalComment/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *paramers=@{
                             @"platform": kPlatform,
                             @"version":kVersion,
                             @"type":self.type,
                             @"comment":self.commentTextView.text,
                             @"overall":[NSString stringWithFormat:@"%d",self.wholeIndex],
                             @"attitude":[NSString stringWithFormat:@"%d",self.severIndex-10],
                             @"effect":[NSString stringWithFormat:@"%d",self.levelIndex-20],
                             };
    if ([self.type isEqualToString:@"1"]) {
        [paramers setValue:self.commentId forKey:@"hospitalid"];
    }else if ([self.type isEqualToString:@"2"])
    {
        [paramers setValue:self.commentId forKey:@"hospdeptid"];
    }
    else if ([self.type isEqualToString:@"3"])
    {
       [paramers setValue:self.commentId forKey:@"doctorid"];
    }
    [manager POST:postURL parameters:paramers success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hiddenActionView];
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
        if ([status isEqualToString:@"1"]) {
            //推出新界面
            if (self.commDelegate&&[self.commDelegate respondsToSelector:@selector(reloadNewdata)]) {
                [self.commDelegate reloadNewdata];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        [self shouLabel:@"评论失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (IBAction)selectWhole:(UIButton *)sender {
    for (int i=1; i<=sender.tag; i++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:i];
        [btn setImage:[UIImage imageNamed:@"评价_选中@2x.png"] forState:UIControlStateNormal];
        if (sender.tag<=3) {
            self.wholeComment.text=@"一般";
        }else if (sender.tag==4)
        {
            self.wholeComment.text=@"满意";
        }else if(sender.tag==5)
        {
            self.wholeComment.text=@"非常满意";
        }
    }
    for (int j=sender.tag+1; j<=5; j++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:j];
        [btn setImage:[UIImage imageNamed:@"评价_未选中@2x.png"] forState:UIControlStateNormal];
    }
    self.wholeIndex=sender.tag;
}
- (IBAction)selectSever:(UIButton *)sender {
    for (int i=11; i<=sender.tag; i++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:i];
        [btn setImage:[UIImage imageNamed:@"评价_选中@2x.png"] forState:UIControlStateNormal];
        if (sender.tag<=13) {
            self.severComment.text=@"一般";
        }else if (sender.tag==14)
        {
            self.severComment.text=@"满意";
        }else if(sender.tag==15)
        {
            self.severComment.text=@"非常满意";
        }
    }
    for (int j=sender.tag+1; j<=15; j++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:j];
        [btn setImage:[UIImage imageNamed:@"评价_未选中@2x.png"] forState:UIControlStateNormal];
    }
    self.severIndex=sender.tag;
}
- (IBAction)selectLevel:(UIButton *)sender {
    for (int i=21; i<=sender.tag; i++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:i];
        [btn setImage:[UIImage imageNamed:@"评价_选中@2x.png"] forState:UIControlStateNormal];
        if (sender.tag<=23) {
            self.levelComment.text=@"一般";
        }else if (sender.tag==24)
        {
            self.levelComment.text=@"满意";
        }else if(sender.tag==25)
        {
            self.levelComment.text=@"非常满意";
        }
    }
    for (int j=sender.tag+1; j<=25; j++) {
        UIButton *btn=(UIButton *)[self.view viewWithTag:j];
        [btn setImage:[UIImage imageNamed:@"评价_未选中@2x.png"] forState:UIControlStateNormal];
    }
    self.levelIndex=sender.tag;
}
- (void)keyboardWillHidden:(NSNotification *)not
{
    if (kScreenHeight==480) {
        [UIView animateWithDuration:0.15 animations:^{
            self.view.frame=CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-150);
        }];
    }
    [self.commentTextView resignFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self hiddenLabel];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
